package com.wx.pay.util;


import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @author Kim
 *
 */
public class MapUtil {
	 
	/**
	 * 解析百度地图返回的经纬度数据
	 * @param sms
	 * @return
	 * @throws Exception
	 */
	public synchronized static Double[] parseJson(String sms) throws Exception{
		
		JSONObject object = JSON.parseObject(sms);
		if(object.getString("status").equals("0")){
			
			object = object.getJSONObject("result").getJSONObject("location");
			
			Double[] d = new Double[]{object.getDouble("lng"),object.getDouble("lat")}; 
			
			return d;
		}
		
		return null;
	}
 
	/**
	 * map添加成数组值
	 * @param data
	 * @return
	 */
	public static Map<String,String[]> stringMapAddArray(Map<String,String> data){
		if(data==null){
			return null;
		}
		Map<String,String[]> result = new HashMap();
		for(Entry<String, String> row : data.entrySet()){
			result.put(row.getKey(), new String[]{row.getValue()});
		}
		return result;
	} 
	
	/**
	 * 字符串解析成map
	 * @param content 字符串内容
	 * @param joinKey 连接符号，用于分割元素
	 * @param kvKey 连接符号，用于分割键值
	 * @param resultClass 返回的map class 可以为空，默认HashMap
	 * @return
	 */
	public static Map<String,String> split(String content, String joinKey, String kvKey, Class<? extends Map> resultClass){
		try {
			Map<String,String> result = resultClass==null?new HashMap():resultClass.newInstance();
			if(StringUtil.nullString(content)){
				return result;
			}
			String[] items = content.split(joinKey);
			for(String item : items){
				String[] kvs = item.split(kvKey);
				result.put(kvs[0], kvs.length>1?kvs[1]:null);
			}
			return result;
		} catch (Exception e) {
			throw new RuntimeException("", e);
		}
	}
	
	
	/**
	 * 合并多个Map
	 * @param resultClass
	 * @param sources
	 * @return
	 */
	public static Map<String,String> merge(Class<? extends Map> resultClass, Map<String,String>... sources){
		try {
			Map<String,String> result = resultClass==null?new HashMap():resultClass.newInstance();
			for(Map<String,String> source : sources){
				for(Entry<String,String> row : source.entrySet()){
					result.put(row.getKey(), row.getValue());
				}
			}
			return result;
		}catch (Exception e) {
			throw new RuntimeException("", e);
		}
	}
	
	/**
	 * 合并多个Map
	 * @param resultClass
	 * @param sources
	 * @return
	 */
	public static Map<String,String> merge(Class<? extends Map> resultClass, Map<String,String> source, String... kvs){
		try {
			Map<String,String> result = resultClass==null?new HashMap():resultClass.newInstance();
			for(Entry<String,String> row : source.entrySet()){
				result.put(row.getKey(), row.getValue());
			}
			String key = null;
			for(String item : kvs){
				if(key!=null){
					result.put(key, item);
					key = null;
				}else{
					key = item;
				}
			}
			return result;
		}catch (Exception e) {
			throw new RuntimeException("", e);
		}
	}
	
	/**
	 * map list 转换为Map
	 * @param list
	 * @param nameKeys
	 * @param valueKey
	 * @return
	 */
	public static Map<String,Object> listToMap(List<Map> list, String[] nameKeys, String valueKey){
		return listToMap(list, nameKeys, null, valueKey, null);
	}
	
	/**
	 * map list 转换为Map
	 * @param list
	 * @param nameKey
	 * @param valueKey
	 * @return
	 */
	public static Map<String,Object> listToMap(List<Map> list, String nameKey, String valueKey){
		return listToMap(list, new String[]{nameKey}, null, valueKey, null);
	}
	
	
	/**
	 * map list 转换为Map
	 * @param list
	 * @param nameKeys
	 * @param valueKey
	 * @return
	 */
	public static Map<String,Object> listToMap(List<Map> list, String[] nameKeys){
		return listToMap(list, nameKeys, null, null, null);
	}
	
	/**
	 * map list 转换为Map
	 * @param list
	 * @param nameKey
	 * @param valueKey
	 * @return
	 */
	public static Map<String,Object> listToMap(List<Map> list, String nameKey){
		return listToMap(list, new String[]{nameKey}, null, null, null);
	}
	
	/**
	 * map list 转换为Map
	 * @param list
	 * @param nameKeys
	 * @param keyJoinStr
	 * @param valueKey
	 * @param mapClass
	 * @return
	 */
	public static Map<String,Object> listToMap(List<Map> list, String[] nameKeys, String keyJoinStr, String valueKey, Class<? extends Map> mapClass){
		if(list==null){
			return new HashMap(0);
		} 
		try {
			Map result = mapClass==null?new HashMap():mapClass.newInstance();
			for(Map row : list){
				StringBuffer key = new StringBuffer();
				String m = "";
				for(String nameKey : nameKeys){
					key.append(m).append(row.get(nameKey));
					m = keyJoinStr==null?"":keyJoinStr;
				}
				result.put(key.toString(), valueKey==null?row : row.get(valueKey));
			}
			return result;
		} catch (InstantiationException e) {
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e);
		}
	}
}
